package net.dopan.pigframe.core.utils;

import net.dopan.pigframe.core.redis.serializer.FastJsonRedisSerializer;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Package: net.dopan.pigframe.core.utils
 * @Title: RedisUtils
 * @Description: 
 * @author: 刘宽
 * @date: 2018/9/6 14:46
 */
public class RedisUtils {

    private static FastJsonRedisSerializer<Object> valueSerializer;
    private static StringRedisSerializer keySerializer;
    private static RedisTemplate<String, Object> redisTemplate;

    static {
        redisTemplate = SpringContextUtils.getBean("redisTemplate");
        valueSerializer = new FastJsonRedisSerializer(Object.class);
        keySerializer = new StringRedisSerializer();
    }

    public static void delete(String key) {
        redisTemplate.delete(key);
    }

    public static void delete(List<String> keys) {
        redisTemplate.delete(keys);
    }

    public static boolean hasKey(final String key) {
        return redisTemplate.hasKey(key);
    }

    public static boolean set(final String key, final Object object) {
        return redisTemplate.execute((RedisCallback<Boolean>) connection -> {
            connection.set(keySerializer.serialize(key), valueSerializer.serialize(object));
            return true;
        });
    }

    public static Object get(final String key) {
        return redisTemplate.execute((RedisCallback<Object>) connection -> {
            byte[] value = connection.get(keySerializer.serialize(key));
            return value == null ? null : valueSerializer.deserialize(value);
        });
    }

    public static String getString(final String key) {
        return redisTemplate.execute((RedisCallback<String>) connection -> {
            byte[] value = connection.get(keySerializer.serialize(key));
            return value == null ? null : (String) valueSerializer.deserialize(value);
        });
    }

    public static boolean expire(final String key, long expire) {
        return redisTemplate.expire(key, expire, TimeUnit.SECONDS);
    }

    public static long lPush(final String key, Object obj) {
        return redisTemplate.execute((RedisCallback<Long>) connection -> {
            long count = connection.lPush(keySerializer.serialize(key), valueSerializer.serialize(obj));
            return count;
        });
    }

    public static long rPush(final String key, Object obj) {
        return redisTemplate.execute((RedisCallback<Long>) connection -> {
            long count = connection.rPush(keySerializer.serialize(key), valueSerializer.serialize(obj));
            return count;
        });
    }

    public static String lPop(final String key) {
        return redisTemplate.execute((RedisCallback<String>) connection -> {
            byte[] res = connection.lPop(keySerializer.serialize(key));
            return (String) valueSerializer.deserialize(res);
        });
    }

    public static String rPop(final String key) {
        return redisTemplate.execute((RedisCallback<String>) connection -> {
            byte[] res = connection.rPop(keySerializer.serialize(key));
            return (String) valueSerializer.deserialize(res);
        });
    }

    public static Long incrBy(String key, Integer value) {
        return redisTemplate.opsForValue().increment(key, value);
    }

}
